package com.example.demo.util;

public class EncryptionFactory {
	//签名
	public static String sign(String msg,String pfxPath,String pfxPwd){
		RsaP1Util rsa = new RsaP1Util();
		String sign = null;
		try {
			sign = rsa.rsaP1Sign(msg, pfxPath, pfxPwd);
		} catch (Exception e) {
			e.getStackTrace();
		}
		return sign;
	}
	//加密
	public static String encrypt(String msg,String cerPath){
		//读取公钥
		String platform_publicKey = TestHelper.readFile(cerPath);
		RsaP1Util rsa = new RsaP1Util();
		String envelopdata = null;
		try {
			envelopdata = new String(rsa.envelopMessageByCert(msg, platform_publicKey));
		} catch (Exception e) {
			e.getStackTrace();
		}
		return envelopdata;
	}
	//解密
	public static String decrypt(String encryptedMsg,String pfxPath,String pfxPwd){
		RsaP1Util rsa = new RsaP1Util();
		String msg = null;
		try {
			msg = new String(rsa.openMessage(encryptedMsg, pfxPath, pfxPwd));//返回数据
		} catch (Exception e) {
			e.getStackTrace();
		}
		return msg;
	}
	/**
	 * 验签
	 * @param data 原报文
	 * @param sign 签名数据
	 * @return
	 */
	public static boolean msg(String data, String sign,String cerPath){
		String publicKey = TestHelper.readFile(cerPath);
		RsaP1Util rsa = new RsaP1Util();
		boolean verify = false;
		try {
			verify = rsa.rsaP1Verify(sign, data, publicKey.getBytes("UTF-8"));
		} catch (Exception e) {
			e.getStackTrace();
		}
		return verify;
	}
}
